//
//  CommentLineInfoView.m
//  backpack
//
//  Created by wujian on 16/9/5.
//  Copyright © 2016年 wujian. All rights reserved.
//

#import "CommentLineInfoView.h"

@interface CommentLineInfoView()

@property(nonatomic,strong)NSMutableArray *btnArray;

@end

@implementation CommentLineInfoView

-(id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.count = 0;
        
        self.btnArray = [NSMutableArray array];
        
        UILabel *commentLabel = [[UILabel alloc] init];
        commentLabel.textColor = CSColorFromRGB(0x999999);
        commentLabel.text = @"4.2";
        commentLabel.font = MidFont;
        [self addSubview:commentLabel];
        self.commentLabel = commentLabel;
        
        UILabel *commentCountLabel = [[UILabel alloc] init];
        commentCountLabel.textColor = CSColorFromRGB(0x999999);
        commentCountLabel.text = @"评价27";
        commentCountLabel.font = MidFont;
        [self addSubview:commentCountLabel];
        self.commentCountLabel = commentCountLabel;
        
        UILabel *courseCountLabel = [[UILabel alloc] init];
        courseCountLabel.font = MidFont;
        courseCountLabel.textColor = CSColorFromRGB(0x999999);
        courseCountLabel.text = @"参课172人";
        [self addSubview:courseCountLabel];
        self.courseCountLabel = courseCountLabel;
        
        UIButton *btn1 = [[UIButton alloc] init];
        //courseshare_icon_star_dis
        btn1.selected = YES;
        [btn1 setImage:@"courseshare_icon_star_dis"];
        [btn1 setSelectedImage:@"courseshare_icon_star_default"];
        btn1.userInteractionEnabled = NO;
        [btn1 addTarget:self action:@selector(btnClick:)];
        btn1.tag = 1;
        [self addSubview:btn1];
        [self.btnArray addObject:btn1];
        self.btn1 = btn1;
        
        UIButton *btn2 = [[UIButton alloc] init];
        btn2.selected = YES;
        [btn2 setImage:@"courseshare_icon_star_dis"];
        [btn2 setSelectedImage:@"courseshare_icon_star_default"];
        btn2.userInteractionEnabled = NO;
        [btn2 addTarget:self action:@selector(btnClick:)];
        btn2.tag = 2;
        [self addSubview:btn2];
        [self.btnArray addObject:btn2];
        self.btn2 = btn2;
        
        UIButton *btn3 = [[UIButton alloc] init];
        btn3.selected = YES;
        [btn3 setImage:@"courseshare_icon_star_dis"];
        [btn3 setSelectedImage:@"courseshare_icon_star_default"];
        btn3.userInteractionEnabled = NO;
        [btn3 addTarget:self action:@selector(btnClick:)];
        btn3.tag = 3;
        [self addSubview:btn3];
        [self.btnArray addObject:btn3];
        self.btn3 = btn3;
        
        UIButton *btn4 = [[UIButton alloc] init];
        btn4.selected = YES;
        [btn4 setImage:@"courseshare_icon_star_dis"];
        [btn4 setSelectedImage:@"courseshare_icon_star_default"];
        btn4.userInteractionEnabled = NO;
        [btn4 addTarget:self action:@selector(btnClick:)];
        btn4.tag = 4;
        [self addSubview:btn4];
        [self.btnArray addObject:btn4];
        self.btn4 = btn4;
        
        UIButton *btn5 = [[UIButton alloc] init];
        btn5.selected = YES;
        [btn5 setImage:@"courseshare_icon_star_dis"];
        [btn5 setSelectedImage:@"courseshare_icon_star_default"];
        btn5.userInteractionEnabled = NO;
        [btn5 addTarget:self action:@selector(btnClick:)];
        btn5.tag = 5;
        [self addSubview:btn5];
        [self.btnArray addObject:btn5];
        self.btn5 = btn5;
    }
    
    return self;
}

-(void)layoutSubviews{
    
    
    [self.btn1 mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self);
        
        make.left.mas_equalTo(self).offset(20);
        
        //make.width.height.mas_equalTo(15);
    }];
    
    [self.btn2 mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.btn1);
        
        make.left.mas_equalTo(self.btn1.mas_right).offset(10);
        
        make.width.height.mas_equalTo(self.btn1);
    }];
    
    [self.btn3 mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.btn2);
        
        make.left.mas_equalTo(self.btn2.mas_right).offset(10);
        
        make.width.height.mas_equalTo(self.btn1);
    }];
    
    [self.btn4 mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.btn3);
        
        make.left.mas_equalTo(self.btn3.mas_right).offset(10);
        
        make.width.height.mas_equalTo(self.btn1);
    }];
    
    [self.btn5 mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.btn4);
        
        make.left.mas_equalTo(self.btn4.mas_right).offset(10);
        
        make.width.height.mas_equalTo(self.btn1);
    }];
    
    [self.commentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.btn5);
        
        make.left.mas_equalTo(self.btn5.mas_right).offset(10);
    }];
    
    [self.commentCountLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.btn1.mas_bottom).offset(12);
        
        make.left.mas_equalTo(self.courseCountLabel.mas_right).offset(10);
    }];
    
    [self.courseCountLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.commentCountLabel);
        
        make.left.mas_equalTo(self.btn1);
        
        make.bottom.mas_equalTo(self).offset(-15);
    }];
}

#pragma mark 按钮点击
-(void)btnClick:(UIButton *)btn{

    btn.selected = !btn.selected;
    
    NSInteger tag = btn.tag;
    
    if (btn.selected) {
        
        for (int i = 0; i < tag; i++) {
         
            UIButton *btn = self.btnArray[i];
            
            btn.selected = YES;
        }
    }
    else{
    
        for (int i = (int)tag - 1; i < self.btnArray.count; i++) {
            
            UIButton *btn = self.btnArray[i];
            
            btn.selected = NO;
        }
        
    }
    
    
}

#pragma mark 计算按钮
-(void)countBtn{

    self.count = 0;
    
    for (UIButton *btn in self.btnArray) {
        
        if (btn.selected) {
            
            self.count = self.count + 1;
        }
    }
}
@end
